package fizzpicks.tetrapulp;

import pulpfizz.pulp.body.Java2DSprite;

import java.awt.*;

import fizzpicks.tetrapulp.tetrapulp.*;
import net.jscience.math.MathFP;

/**
 * Created by will
 * Date: May 9, 2008 9:52:27 PM
 */
public class LJViewSprite extends Java2DSprite {
    public LJView v;

    public LJViewSprite(int x, int y, int w, int h, LJView v) {
        super(x, y, w, h);
        this.v = v;
    }

    protected boolean needsRedraw() {
        return true;
    }

    protected void draw(Graphics2D g) {
        LJField p = v.field;
        for (int y = 0; y < Lockjaw.LJ_PF_HT/* && !rows.isEmpty()*/; ++y) {
          int blankTile = 0;

/*
          // hotline: draw 0x100 as the background
          if (hotlineRows[y]!=0 && v.field.scoreStyle == LJSCORE.HOTLINE) {
            blankTile = 0x100;
          }
          // during line clear delay, draw bars to show which lines were cleared
          if (p.state == LJState.LINES_FALLING && p.stateTime > 0 && p.tempRows.get(y)) {
            blankTile = 0x100;
          }
*/

          if (true/*rows.get(y)*/) {
            for (int x = p.leftWall; x < p.rightWall; x++) {
              int b = v.hidePF ? 0 : p.b[y][x];
              drawBlock(g,x, Lockjaw.LJ_PF_HT-y, b != 0 ? b : blankTile);
            }
          }
        }
        drawFallingPiece(g);
    }

    private void drawFallingPiece(Graphics2D g) {
        final LJField p = v.field;
        LJP piece = p.curPiece[0];
        if(piece == null) return;
        int y = v.smoothGravity
                ? (int)p.y
                : (int) MathFP.toFP(MathFP.toLong(p.y));
        y = (int)p.y;
        final int color = (p.state == LJState.LANDED)
                          ? -128 - ((p.stateTime + 1) * 128 / (p.speed.lockDelay + 1))
                          : Lockjaw.pieceColors[piece.index];

        // Draw trails
        if (v.showTrails) {

          // trail going up
          while (v.trailY - y < MathFP.toFP(-1)) {
            v.trailY += MathFP.toFP(1);
            drawPieceInPF(g,p.x, v.trailY, p.theta, color, piece);
          }

          // trail going down
          while (v.trailY - y > MathFP.toFP(1)) {
            v.trailY -= MathFP.toFP(1);
            drawPieceInPF(g,p.x, v.trailY, p.theta, color, piece);
          }
        }
        drawPieceInPF(g,p.x, y, p.theta, color, piece);
        v.trailY = y;
    }

    /**
     * Draws a tetromino to the playing field.
     * @param x location
     * @param y location
     * @param theta the rotation state (0: U; 1: R; 2: D; 3: L; 4: Initial position)
     * @param color Drawing style
     * color == 0: draw shadow
     * color == 0x10 through 0x70: draw in that color
     * color == 0x80: draw as garbage
     * color == -255 through -1: draw with 255 through 1 percent lighting
     * @param piece piece to draw
     */
    private void drawPieceInPF(Graphics2D g,int x, long y, int theta, int color, LJP piece) {
        // Don't try to draw the -1 that's the sentinel for no hold piece
        if (piece == LJP.NONE)
          return;

        LJBits rows = new LJBits();
        LJBlkSpec[] blocks = new LJBlkSpec[4];
        for (int i = 0; i < blocks.length; i++) { blocks[i] = new LJBlkSpec(); }
//        final int srcW = v.plat.skin.blkW;
//        final int srcH = v.plat.skin.blkH;
//        Image singleBlocks = v.plat.skin.blocks;
//        int singleSeries = (color == 0
//                            && singleBlocks.getHeight(null) >= 6 * srcH)
//                           ? 4 : 2;
//
//        // color: 0 for shadow, >0 for piece
//        Image connBlocks = (color != SHADOW_BLOCK)
//                             ? v.plat.skin.connBlocks : null;
//
//        Image transTemp = color < 0
//                            || (color == SHADOW_BLOCK && v.hideShadow.trans)
//                            ? create_bitmap(w, h) : null;
//
//        if (transTemp != null) {
//          set_trans_blender(0, 0, 0, v.hideShadow != LJSHADOW.NONE ? 128 : 64);
//        }

        Lockjaw.expandPieceToBlocks(blocks, v.field, piece, 0, 0, theta);
//        acquire_bitmap(b);
        for (int blk = 0; blk < 4; ++blk) {
          int blkValue = blocks[blk].conn;

          // If blkValue == 0 then the block is not part of the piece,
          // such as if it's a domino or tromino or (once pentominoes
          // are added) a tetromino.
          if (blkValue != 0) {
            int blkX = blocks[blk].x;
            int blkY = blocks[blk].y;


/*
            final int dstX = x + w * blkX;
            final int dstY = y + h * (-1 - blkY);

            if (color > 0) {
              blkValue = color | (blkValue & Lockjaw.CONNECT_MASK);
            } else if (color == 0 && v.hideShadow == LJSHADOW.COLORLESS) {
              blkValue = 0;
            }

            if (connBlocks != null) {
              final int srcX = ((blkValue >> 0) & 15) * srcW;
              final int srcY = ((blkValue >> 4) & 15) * srcH;

              if (transTemp != null) {
                stretch_blit(connBlocks, transTemp,
                             srcX, srcY, srcW, srcH,
                             0, 0, w, h);
                if (color < 0) {
                  draw_lit_sprite(b, transTemp, dstX, dstY, color + 256);
                } else {
                  draw_trans_sprite(b, transTemp, dstX, dstY);
                }
              } else if (srcW != w || srcH != h) {
                stretch_blit(connBlocks, b,
                             srcX, srcY, srcW, srcH,
                             dstX, dstY, w, h);
                if (dstY > 600) {
                  allegro_message("dstY = "+dstY+"\n");
                }
              } else {
                blit(connBlocks, b,
                     srcX, srcY,
                     dstX, dstY, w, h);
              }
            } else {
              final int srcX = ((blkValue >> 4) & 7) * srcW;
              final int srcY = (((blkValue >> 7) & 1) + singleSeries) * srcH;

              if (transTemp != null) {
                stretch_blit(singleBlocks, transTemp,
                             srcX, srcY, srcW, srcH,
                             0, 0, w, h);
                if (color < 0) {
                  draw_lit_sprite(b, transTemp, dstX, dstY, color + 256);
                } else {
                  draw_trans_sprite(b, transTemp, dstX, dstY);
                }
              } else {
                stretch_blit(singleBlocks, b,
                             srcX, srcY, srcW, srcH,
                             dstX, dstY, w, h);
              }
            }
*/
              drawFallingBlock(g,blkX+x,MathFP.toFP(blkY)+y,1);
            rows.set(blkY);
          }
        }
//        release_bitmap(b);
//        if (transTemp != null) {
//          solid_mode();
//          transTemp.flush();
//        }

//        return rows;
    }

    private void drawFallingBlock(Graphics2D g, int x, long y, int blk) {
        if(blk == 0) {
            g.setColor(Color.black);
        } else {
            g.setColor(Color.red);
        }
        g.fillRect(x*BLOCK_SIZE,Lockjaw.LJ_PF_HT*BLOCK_SIZE - (int)MathFP.toLong(y*BLOCK_SIZE),BLOCK_SIZE,BLOCK_SIZE);
    }

    public static final int BLOCK_SIZE = 20;
    private void drawBlock(Graphics2D g, int x, int y, int blk) {
        if(blk == 0) {
            g.setColor(Color.black);
        } else {
            g.setColor(Color.red);
        }
        g.fillRect(x*BLOCK_SIZE,y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
    }
}
